#####
## Replication File:
##
## "The Impact of Social Desirability Bias on Conspiracy
## Belief Measurement Across Cultures"
##
## Steven M. Smallpage, Adam M. Enders, Hugo Drochon, and Joseph E. Uscinksi
##
## Published in Political Science Research & Methods
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.3.1093,
## using R 3.6 (Mac OS)
####

library(stats)
library(car)
library(ggplot2)
library(lattice)
library(latticeExtra)
library(foreign)

# Set working directory

# Open data
sdb16 <- read.dta("SDB Data.dta")

################################################################################

####
## Figure 1
####

tiff("figure1.tiff", width=9, height=5.5, units = "in", res=300, compression = 'lzw')
histogram(~as.factor(ctcount) | country,
          data = sdb16,
          aspect = 1,
          xlab = "Number of Beliefs"
          )
dev.off()

################################################################################

####
## Figure 2
####

ctmeans <- read.csv("Proportions by CT.csv", header = TRUE)

tiff("figure2.tiff", width=9, height=5.5, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ country | belief,
       data = ctmeans,
       aspect = 1,
       ylim = c(-0.1, 0.85),
       col = "black",
       pch = 20,
       layout = c(4, 2),
       xlab = " ",
       ylab = "Proportion Expressing Belief",
       scales = list(x = list(labels = c("", "GB", "DE", "SE", "PT", "IT",
                                         "PL", "AR"))),
       panel = function(x, y, subscripts, ...){
         panel.xyplot(x, y, col=...)
         panel.segments(ctmeans$country, ctmeans$lower[subscripts],
                        ctmeans$country, ctmeans$upper[subscripts])
       }
)
dev.off()

################################################################################

####
## Figure 3
####

ctcounts <- read.csv("Counts by Condition.csv", header = TRUE)

ctcounts$order <- factor(ctcounts$order,
                         levels = c(1,2,3,4,5,6,7,8),
                         labels = c("Great Britain", "Germany", "Sweden",
                                    "Portugal", "Italy", "Poland", 
                                    "Argentina", "Pooled"))

tiff("figure3.tiff", width=9, height=5.5, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ condition | order,
       data = ctcounts,
       aspect = 1,
       ylim = c(-0.5, 8.5),
       xlim = c(-.5, 1.5),
       type = c("p", "l"),
       lty = 3,
       layout = c(4, 2),
       col = "black",
       pch = 20,
       xlab = " ",
       ylab = "Number of Beliefs",
       scales = list(x = list(labels = c("Control", "Treatment"), at=c(0, 1), 
                              alternating=FALSE)),
       panel = function(x, y, subscripts, ...){
         panel.xyplot(x, y, col=...)
         panel.segments(ctcounts$condition, ctcounts$lower[subscripts],
                        ctcounts$condition, ctcounts$upper[subscripts])
       }
)
dev.off()

################################################################################

####
## Figure A1
####

N <- seq(200,1050,by=50)

dat2 <- data.frame(n=N)

for (i in seq(N)) {
        two.power<-power.t.test(power=.50, sig.level=.05, sd=1, n=N[i])
        dat2$d[i]<-two.power$delta
}

plt2 <- ggplot(dat2,aes(y=d,x=n, label = round(d,digits = 2))) + stat_smooth(method = "loess",se=FALSE)
plt2 <- plt2 + labs(x="Sample size",y="Cohen's d effect size (Assuming power=0.5)")
plt2 <- plt2 + scale_x_continuous(breaks=seq(min(N),max(N),by=50))
plt2 <- plt2 + geom_label()
tiff("figureA1.tiff", width=9, height=6.5, units = "in", res=300, compression = 'lzw')
plt2
dev.off()

################################################################################

####
## Figure A2
####

N <- seq(200,1050,by=50)

dat <- data.frame(n=N)

# Changes in power
for (i in seq(N)) {
        one.power <- power.t.test(delta=0.2, sig.level=.05, sd=1, n=N[i])
        dat$d[i] <- one.power$power
}

plt <- ggplot(dat,aes(y=d,x=n, label = round(d,digits = 2))) + stat_smooth(method = "loess",se=FALSE)
plt <- plt + labs(x="Sample size",y="Power (Assuming d=0.2)")
plt <- plt + scale_x_continuous(breaks=seq(min(N),max(N),by=50))
plt <- plt + geom_label()
tiff("figureA2.tiff", width=9, height=6.5, units = "in", res=300, compression = 'lzw')
plt
dev.off()


